package com.sale.succ.components;

import java.util.ArrayList;

import com.sale.succ.interfaces.ValueGetter;

import eve.fx.Point;
import eve.ui.ControlConstants;

public class Keyboard extends DefForm{
	private ValueGetter valueGetter;
	private Point cell;
	private DefInput display;
	private static final String BACK_SP = "<-";
	private static final String ENTER = "Ok";
	private static final String CANCEL = "><";
	private static final int MAX_NUMBERS = 8;
	private String startValue;
	public Keyboard(Point cell, ValueGetter valueGetter, String title, String startValue){
		this.valueGetter = valueGetter;
		this.cell = cell;
		this.title = title;
		this.startValue = startValue;
		initGui();
	}
	
	private void initGui(){
		
		this.addLast(getDisplay());
		for (int i = 0; i < getValues().size(); ++i) {
			this.addNext(createCalcButton(String.valueOf(getValues().get(i)))).setControl(TOP);
		}
		this.addNext(getBackSpButton());
		this.addNext(getEnterButton());
		this.addNext(getCancel());
	}
	
	private CalcButton cancelButton;
	
	private CalcButton getCancel(){
		if(cancelButton == null){
			cancelButton = new CalcButton(CANCEL);
			cancelButton.setAction(CANCEL);
		}
		return cancelButton;
	}
	
	private ArrayList getValues() {
		if (values == null) {
			values = new ArrayList();
			values.add("0");
			values.add("1");
			values.add("2");
			values.add("3");
			values.add("4");
			values.add("5");
			values.add("6");
			values.add("7");
			values.add("8");
			values.add("9");
		}
		return values;
	}
	
	private CalcButton backSpButton;
	private CalcButton enterButton;
	
	private CalcButton getBackSpButton(){
		if(backSpButton == null){
			backSpButton = new CalcButton(BACK_SP);
			backSpButton.setAction(BACK_SP);			
		}
		return backSpButton;
	}
	
	private CalcButton getEnterButton(){
		if(enterButton == null){
			enterButton = new CalcButton(ENTER);
			enterButton.setAction(ENTER);			
		}
		return enterButton;
	}
	
	private DefInput getDisplay(){
		if(display == null){
			display = new DefInput();
			display.setText(startValue);
			display.set(ControlConstants.NotEditable, true);
		}
		return display;
	}
	
	private void addToDisplay(String value) {
		String currentValue = getDisplay().getText();
		if (currentValue.length() > MAX_NUMBERS) {
			return;
		}
		if(currentValue.length() == 1 && currentValue.equals("0")){
			currentValue = "";
		}
		currentValue = currentValue + value;
		getDisplay().setText(currentValue);
	}
	
	private void subFromDisplay(){
		String currentValue = getDisplay().getText();
		if(currentValue.length() == 1) {
			getDisplay().setText("0");
			return;
		}
		currentValue = currentValue.substring(0,currentValue.length()-1);
		getDisplay().setText(currentValue);
	}
	
	private CalcButton createCalcButton(String value) {
		CalcButton calcButton = new CalcButton(value);
		calcButton.setAction(value);		
		return calcButton;
	}
	private ArrayList values;
	
	public boolean handleAction(String action){
		if(!action.equals(BACK_SP) && !action.equals(ENTER)){
			if (getValues().contains(action)) {
				addToDisplay(action);
			}
		}
		if(action.equals(BACK_SP)){
			subFromDisplay();
		}
		
		if(action.equals(CANCEL)){
			canExit = true;
			exit(0);
		}
		
		if(action.equals(ENTER)){
			notifyValueGetter(getDisplay().getText());
			canExit = true;
			exit(0);
		}
		return super.handleAction(action);
	}
	
	private boolean canExit = false;
	
	public boolean canExit(int code) {
		return canExit;
	}
	
	private void notifyValueGetter(String value){
		valueGetter.setValue(cell, value);
	}
	
	

}
